<?php
	/*
	*---------------------
	*学生实体的统一管理控制类
	*
	*
	*---------------------
	*/

	class StudentController extends BaseController{

		/**
		 * 返回所有的学生信息
		 *对应路由 Route::resource('student', 'StudentController');
		 * @return Response json
		 */
		public function index()
		{
			
			$column = Input::get('column');
			$content = Input::get('content');

			if(null==$content || null== $column){
				$column ='';
				$content='';
			}

			 $student_query = DB::table("student")
					->join('parents','parents.student_id','=','student.id')
					->join('classes','student.classes_id','=','classes.id')
					->select('student.*',
							'classes.grade as classes_grade',
							'classes.class as classes_class',
							'parents.name as parents_name',
							'parents.tel as parents_tel',
							'parents.relation as relation',
							'parents.nativeid as parents_nativeid'
					 		)
					->where('student.is_del',0)
					->orderBy('student.created_at','ASC');
	
			if(''!= $column){

                switch ($column) {
                    case 'classes_id':
                        $table_name = 'student';
                        $column = 'classes_id';
                        $student_query = $student_query->where($table_name.'.'.$column, 
                										'=',$content);
                        break;
                    case 'name';
                        $table_name = 'student';
                        $column = 'name';
                        $student_query = $student_query->where($table_name.'.'.$column, 
                										'like', '%'.$content.'%');
                        break;

                    case 'stuid';
                   	    $table_name = 'student';
                        $column = 'stuid';
                        $student_query = $student_query->where($table_name.'.'.$column, 
                										'like', '%'.$content.'%');
                }//switch
            }//if



			$result = $student_query
				->orderBy('created_at','ASC')->paginate(15)
					->toJson();
          

			//搜索结果的编解码，如果缺少这个步骤获取到的将是空值。
			$result = json_decode($result);

			return Response::json(['error'=>false, 'result'=>$result]);

		}//function index()

		/**
		 * 学生详情 
		 * @param 
		 * @return Response
		 */
		public function show($id){
			$student_query = DB::table("student")
					->join('parents','parents.student_id','=','student.id')
					->join('classes','student.classes_id','=','classes.id')
					->select('student.*',
							'classes.grade as classes_grade',
							'classes.class as classes_class',
							'parents.name as parents_name',
							'parents.tel as parents_tel',
							'parents.relation as relation',
							'parents.nativeid as parents_nativeid'
					 		)
					->where('student.id',$id)
					->get();
			return	Response::json(['error'=>false, 'result'=>$student_query]);	
		}

		/**
		 * 创建学生实体函数
		 *对应路由 Route::resource('student', 'StudentController');
		 * @return Response
		 */
		public function store(){

			if(!Input::has('name') || !Input::has('sex') || !Input::has('stuid') ||
			   !Input::has('classes_id')){
				return Response::json(['error'=>true,'message'=>'缺少重要参数']);
			}
			$new_student_arr = array();
			$message = Input::all();
			$student_message = array('name','sex','stuid','classes_id','remark');
			foreach ($message as $key => $value) {
				if(in_array($key,$student_message)){
					$new_student_arr[$key] = $value;
				}
			}
			//开启事务
			DB::beginTransaction();
			//判断学生是否存在
			$has_student = DB::table('student')
							->where('stuid','=', Input::get('stuid'))
							->where('name' , '=', Input::get('name'))
							->count();

			//如果已有记录则报错返回
			if($has_student){
				return Response::json(['error'=>true,'message'=>'该生已存在']);
			}

			$new_student_id = DB::table('student')->insertGetId($new_student_arr);
			if(!$new_student_id){
				DB::rollback();
			}
			$new_parents = array(
				'name' => Input::get('parent_name'),
				'relation' => Input::get('relation'),
				'tel' => Input::get('tel'),
				'nativeid' => Input::get('nativeid'),
				'student_id' => $new_student_id
				);
			$new_parents_id = DB::table('parents')->insertGetId($new_parents);
			if(!$new_parents_id){
				DB::rollback();
			}
			//修改班级人数
			$student_num = DB::table('classes')->select('student_num')
							->where('id',Input::get('classes_id'))->get();
			$new_classes_data = DB::table('classes')->where('id',Input::get('classes_id'))
								->update(array('student_num'=>$student_num[0]->student_num+1));
			if(!$new_classes_data){
				DB::rollback();
			}					
			DB::commit();
			return Response::json(['error'=>false,'message'=>'添加成功'],200);
		}



		/**
		 * 修改学生实体数据
		 * 对应路由 Route::resource('student', 'StudentController');
		 * @param int  $id
		 * @return Response
		 */
		public function update($id){
			DB::beginTransaction();
			$input_param = Input::all();
	 	
		 	$stu_relevant_param = array('name','stuid','sex','classes_id','remark');

		 	$stu_update_arr = array();

		 	foreach ($input_param as $key => $val){
		 		if(in_array($key, $stu_relevant_param)){
		 			$stu_update_arr[$key] = $val;
		 		}
		 	}
		 	$stu_result = DB::table('student')->where('id','=',$id)->update($stu_update_arr);
			if(!$stu_result){
				DB::rollback();
			}
			$par_relevant_param = array('relation','tel','nativeid');
			//$par_relevant_param['name'] = Input::get('parents_name');
			$par_update_arr = array();

		 	foreach ($input_param as $key => $val){
		 		if(in_array($key, $par_relevant_param)){
		 			$par_update_arr[$key] = $val;
		 		}
		 	}
		 	$par_update_arr['name'] = Input::get('parents_name');
		 	$par_result = DB::table('parents')->where('student_id','=',$id)->update($par_update_arr);
		 	if(!$par_result){
				DB::rollback();
			}
			DB::commit();
			return Response::json(['error'=>false,'message'=>'修改成功'],200);
		}

		/**
		 * 删除学生实体数据
		 * 对应路由 Route::resource('student', 'StudentController');
		 * @param int  $id
		 * @return Response
		 */
		public function destroy($id){
			DB::beginTransaction();
			$del_student = DB::table('student')->where('id',$id)->update(array('is_del'=>1));
			if(!$del_student){
				DB::rollback();
			}
			$del_parents = DB::table('parents')->where('student_id',$id)->update(array('is_del'=>1));
			if(!$del_parents){
				DB::rollback();
			}
			$classes_id = DB::table('student')->select('classes_id')->where('id',$id)->get();
			$student_num = DB::table('classes')->select('student_num')->where('id',$classes_id[0]->classes_id)->get();
			$classes_new_array = array(
				'student_num' => $student_num[0]->student_num-1
				);
			$classes_update = DB::table('classes')->where('id',$classes_id[0]->classes_id)->update($classes_new_array);
			if(!$classes_update){
				DB::rollback();
			}
			DB::commit();
			return Response::json(['error'=>false,'message'=>'删除成功'],200);
		}

	}

?>